package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = (Page<DishVO>) dishMapper.pageQuery(dishPageQueryDTO);
        for (DishVO dishVO : page) {
            dishVO.setCategoryName(dishMapper.getCategoryName(dishVO.getCategoryId()));
        }
        return new PageResult(page.getTotal(), page.getResult());
    }
    @Override
    public List<DishVO> getDishByCategoryId(Long categoryId) {
        List<DishVO> dishVOList = dishMapper.getDishByCategoryId(categoryId);
        for (DishVO dishVO : dishVOList) {
            dishVO.setCategoryName(dishMapper.getCategoryName(dishVO.getCategoryId()));
            dishVO.setFlavors(dishMapper.getFlavors(dishVO.getId()));
        }
        return dishVOList;
    }

    @Override
    public List<DishVO> list(Long categoryId) {
        return dishMapper.getDishByCategoryId(categoryId);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        dishMapper.updateStatus(status, id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            dishMapper.delete(id);
            dishMapper.deleteFlavors(id);
        }
    }

    @Override
    public DishVO getById(Long id) {
        Dish dish = dishMapper.getById(id);
        List<DishFlavor> flavors = dishMapper.getFlavors(id);
        DishVO dishVO = DishVO.builder()
                .id(dish.getId())
                .name(dish.getName())
                .categoryId(dish.getCategoryId())
                .price(dish.getPrice())
                .image(dish.getImage())
                .description(dish.getDescription())
                .status(dish.getStatus())
                .updateTime(dish.getUpdateTime())
                .build();
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    @Override
    public void add(DishDTO dishDTO) {
        Dish dish = new Dish();
        dish.setName(dishDTO.getName());
        dish.setCategoryId(dishDTO.getCategoryId());
        dish.setPrice(dishDTO.getPrice());
        dish.setImage(dishDTO.getImage());
        dish.setDescription(dishDTO.getDescription());
        dish.setStatus(dishDTO.getStatus());

        dishMapper.addDish(dish);
        Long id = dish.getId();
        for (DishFlavor dishFlavor : dishDTO.getFlavors()) {
            dishFlavor.setDishId(id);
            dishMapper.addDishFlavor(dishFlavor);
        }
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        dish.setId(dishDTO.getId());
        dish.setName(dishDTO.getName());
        dish.setCategoryId(dishDTO.getCategoryId());
        dish.setPrice(dishDTO.getPrice());
        dish.setImage(dishDTO.getImage());
        dish.setDescription(dishDTO.getDescription());
        dish.setStatus(dishDTO.getStatus());
        
        dishMapper.update(dish);
        dishMapper.deleteFlavors(dishDTO.getId());
        for (DishFlavor dishFlavor : dishDTO.getFlavors()) {
            dishFlavor.setDishId(dishDTO.getId());
            dishMapper.addDishFlavor(dishFlavor);
        }
    }
}
